package net.shop.controller;

import javax.servlet.http.HttpServletRequest;

import net.shop.common.SystemConstants;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.stereotype.Controller;

import ths.web.action.Action;
import ths.web.action.ActionContext;
import ths.web.action.ActionException;
import ths.web.action.ActionSupport;
import ths.web.action.exception.ActionNotFoundException;
import ths.web.controller.BackendActionController;
import ths.web.spring.BeansManager;

@Controller("backendActionController")
public class ShopBackendController extends BackendActionController {
	
	private static final String ACTION_SUFFIX = "Action";
	private static final char UNDERSCORE = '-';
	private static final char URL_SPLIT = '/';
	private static final char POINT = '.';
	
	public void prepareActionContext() throws ActionException {
		ActionContext context = ActionContext.getContext();
		HttpServletRequest request = context.getRequest();
		context.setDomain(SystemConstants.WEB_DOMAIN);
		
		Action action = getAction(request);
		context.setActionInstance(action);
		context.setActionNamespace(action.getNamespace());
	}

	public String getViewResourceFullPath(String viewName) {
		StringBuffer path = new StringBuffer("admin/");
		path.append(getTheme()).append('/');
		path.append(viewName).append(".html");
		
		return path.toString();	
	}
	
	private String getTheme() {
		return "default";
	}
	
	private ActionSupport getAction(HttpServletRequest request) throws ActionException {
		String beanName = getActionBeanName(request);
		if (StringUtils.isEmpty(beanName)) {
			throw new ActionNotFoundException(" Not found action name.");
		}
		
		Object obj;
		try {	
			obj = BeansManager.getBean(beanName);
		} catch (NoSuchBeanDefinitionException e) {
			throw new ActionNotFoundException("Not found bean["+ beanName +"] in spring.");
		}
		
		if (obj == null || !(obj instanceof ActionSupport)) {
			throw new ActionNotFoundException("is not a http action object.");
		}
		
		return (ActionSupport) obj;
	}
	
	private String getActionBeanName(HttpServletRequest request) {
		boolean hasUpper = false;
		String requestUrl = StringUtils.trimToEmpty(request.getRequestURI());
	    int length = ACTION_SUFFIX.length() + requestUrl.length() + 1;
	    if (length == 1) return requestUrl;
	    
	    int index = 0;
	    if (requestUrl.charAt(index) == URL_SPLIT ) index = 1;
	    	
	    StringBuilder buffer = new StringBuilder(length);
	    for (; index < length; index++) {
			char ch = requestUrl.charAt(index);
			if (ch == POINT) {
				break;
			} else if (Character.isWhitespace(ch)) {
				continue;
			} else if (ch == UNDERSCORE) {
				hasUpper = true;
			} else if (ch == URL_SPLIT) {
				hasUpper = true;
			} else {
				if (hasUpper) {
					buffer.append(Character.toUpperCase(ch));
					hasUpper = false;
				} else {
					buffer.append(Character.toLowerCase(ch));
				}
			}
	    }
	    
	    buffer.append(ACTION_SUFFIX);
	    return buffer.toString();			
	}	
}
